﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PerpheralDevice.Units;
/// <summary>
/// 运动控制卡的运动轴运动IO/状态
/// </summary>
public readonly struct MotorStatus {
    /// <summary>
    /// 凌华运动轴运动IO/状态
    /// </summary>
    /// <param name="bitStatusIO">
    /// Motion IO status bit number define.
    /// MIO_ALM      =(0),   // Servo alarm. ALM 伺服报警
    /// MIO_PEL      =(1),   // Positive end limit.
    /// MIO_MEL      =(2),   // Negative end limit.
    /// MIO_ORG      =(3),   // ORG (Home)
    /// MIO_EMG      =(4),   // Emergency stop: EMG 传感器
    /// MIO_EZ     =(5),   // EZ.
    /// MIO_INP      =(6),   // In position. INP 就位
    /// MIO_SVON       =(7),   // Servo on signal. SVON 伺服开启
    /// MIO_RDY      =(8),   // Ready.
    /// MIO_WARN       =(9),   // Warning.
    /// MIO_ZSP      =(10),  // Zero speed.
    /// MIO_SPEL       =(11),  // Soft positive end limit.
    /// MIO_SMEL       =(12),  // Soft negative end limit.
    /// MIO_TLC      =(13),  // Torque is limited by torque limit value.
    /// MIO_ABSL       =(14),  // Absolute position lost.
    /// MIO_STA      =(15),  // External start signal.
    /// MIO_PSD      =(16),  // Positive slow down signal
    /// MIO_MSD      =(17),  // Negative slow down signal
    /// MIO_SCL      =(10),  // Circular limit.
    /// MIO_OP      =(24),  // Not all slaves are in operation mode.
    /// </param>
    /// <param name="bitStatus">
    /// MTS_CSTP       =(0),     // Command stop signal. CSTP 命令停止（但可能正在运行）
    /// MTS_VM     =(1),     // At maximum velocity.
    /// MTS_ACC      =(2),     // In acceleration.
    /// MTS_DEC      =(3),     // In deceleration.
    /// MTS_DIR      =(4),     // (Last)Moving direction.
    /// NSTP           =(5),     // Normal stop(Motion done): MDN 动作完成。 0：运动中； 1：运动完成（可能是异常停止）
    /// MTS_HMV      =(6),     // In home operation. HMV 处于归零中
    /// MTS_SMV      =(7),     // Single axis move( relative, absolute, velocity move).
    /// MTS_LIP      =(8),     // Linear interpolation.
    /// MTS_CIP      =(9),     // Circular interpolation.
    /// MTS_VS     =(10),    // At start velocity.
    /// MTS_PMV      =(11),    // Point table move.
    /// MTS_PDW      =(12),    // Point table dwell move.
    /// MTS_PPS      =(13),    // Point table pause state.
    /// MTS_SLV      =(14),    // Slave axis move.
    /// MTS_JOG      =(15),    // Jog move.
    /// MTS_ASTP       =(16),    // Abnormal stop.  ASTP 0：正常停止； 1：异常停止；轴运动时，该位清零。
    /// MTS_SVONS    =(17),    // Servo off stopped.
    /// MTS_EMGS       =(18),    // EMG / SEMG stopped.
    /// MTS_ALMS       =(19),    // Alarm stop.
    /// MTS_WANS       =(20),    // Warning stopped.
    /// MTS_PELS       =(21),    // PEL stopped.
    /// MTS_MELS       =(22),    // MEL stopped.
    /// MTS_ECES       =(23),    // Error counter check level reaches and stopped.
    /// MTS_SPELS    =(24),    // Soft PEL stopped.
    /// MTS_SMELS    =(25),    // Soft MEL stopped.
    /// MTS_STPOA    =(26),    // Stop by others axes.
    /// MTS_GDCES    =(27),    // Gantry deviation error level reaches and stopped.
    /// MTS_GTM      =(28),    // Gantry mode turn on.
    /// MTS_PAPB       =(29),    // Pulsar mode turn on.
    /// </param>
    public MotorStatus(int bitMIO, int bitMTS) {
		this.MIO_ALM = (bitMIO & 1 << 0) > 0;
		this.MIO_PEL = (bitMIO & 1 << 1) > 0;
		this.MIO_MEL = (bitMIO & 1 << 2) > 0;
		this.MIO_ORG = (bitMIO & 1 << 3) > 0;
		this.MIO_EMG = (bitMIO & 1 << 4) > 0;
		this.MIO_EZ = (bitMIO & 1 << 5) > 0;
		this.MIO_INP = (bitMIO & 1 << 6) > 0;
		this.MIO_SVON = (bitMIO & 1 << 7) > 0;

        this.MTS_MDN = (bitMTS & 1 << 5) > 0;
    }

	/// <summary>
	/// MIO: 报警
	/// </summary>
	public bool MIO_ALM { get; }
    /// <summary>
    /// MIO: 正极限
    /// </summary>
    public bool MIO_PEL { get; }
    /// <summary>
    /// MIO: 负极限
    /// </summary>
    public bool MIO_MEL { get; }
    /// <summary>
    ///  MIO: 原点
    /// </summary>
    public bool MIO_ORG { get; }
    /// <summary>
    /// MIO: 急停
    /// </summary>
    public bool MIO_EMG { get; }
    /// <summary>
    /// MIO: EZ
    /// </summary>
    public bool MIO_EZ { get; }
    /// <summary>
    /// MIO: 到位(就位)
    /// </summary>
    public bool MIO_INP { get; }
    /// <summary>
    /// MIO: 使能
    /// </summary>
    public bool MIO_SVON { get; }
    /// <summary>
    /// NSTP: MDN动作借宿 0：运动进行； 1：运动结束（可能异常停止）
    /// </summary>
    public bool MTS_MDN {  get; }

	public bool Equals(MotorStatus motorStatus) {
		foreach (var tempProps in this.GetType().GetProperties()) {
			if (!(tempProps.GetValue(this)?.Equals(tempProps.GetValue(motorStatus)) ?? false)) {
				return false;
			}
		}
		return true;
	}

}
